
import './css/index.css'
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
import './css/golbal.css'
import moment from 'moment'
import Vue from 'vue'
//导入vuex
import Vuex from 'vuex'
//安装vuex
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem("car")||"[]");
//创建vuex实例
var store = new Vuex.Store({
	state:{
		car:car
	},
	mutations:{
		addToCar(state,info){
			/* 
			1、数组已经有了该商品，跟新数量即可
			2、数组中没有该商品，加入到数组中
			 */
			var car = state.car;
			var index = car.findIndex(item => item.id==info.id);
			if(index<0){
				state.car.push(info);
			}else{
				state.car[index].count+=parseInt(info.count)
			}
			localStorage.setItem("car",JSON.stringify(state.car))
		},
		updateCount(state,info){
			state.car.some(item => {
				if(item.id == info.id){
					item.count = info.count;
					return true;
				}
			})
			localStorage.setItem("car",JSON.stringify(state.car))
		},
		deleteGoods(state,id){
			state.car.some((item,i) => {
				if(item.id==id){
					state.car.splice(i,1);
					return true;
				}
			}) 
			localStorage.setItem("car",JSON.stringify(state.car))
		},
		updateSelected(state,info){
			state.car.some(item => {
				if(item.id == info.id){
					state.selected = info.val;
					return true;
				}
			})
			localStorage.setItem("car",JSON.stringify(state.car))
		}
	},
	getters:{
		totalCount:function(state){
			var total = 0;
			var car = state.car;
			for(var i=0;i<car.length;i++){
				total+=parseInt(car[i].count);
			}
			return total;
		},
		carList:function(state){
			return state.car;
		}
	}
})
//导入vue-router
import VueRouter from 'vue-router'
//手动安装vue-router
Vue.use(VueRouter)
//导入 vue-resource 
import VueResource from 'vue-resource'
Vue.use(VueResource)
//设置请求的根路径
Vue.http.options.root = ''
//全局设置 post 提交数据的时候，数据格式的组织形式
Vue.http.options.emulateJSON = true;

//导入 vue-preview
import VuePreview from 'vue-preview'
//Vue.use(VuePreview)
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {top: 0, bottom: 0},
  captionEl: false,
  fullscreenEl: false,
  shareEl: true, //分享
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: true //图片拖动切换
})

//导入mint-ui
/* import { Header,Swipe, SwipeItem, Button, Lazyload} from 'mint-ui'
Vue.component(Header.name,Header)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name,Button)
//按需导入的方式，无法加载，懒加载的小图标
Vue.use(Lazyload) */

//全部导入mint-ui
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//定义全局的过滤器
Vue.filter('dateFormat',function(dateStr,pattern='YYYY-MM-DD HH:mm:ss'){
	//使用moment 来格式化时间
	return moment(dateStr).format(pattern);
})

//导入 router.js 文件
import router from './router.js'

import indexV from './index.vue'


var app = new Vue({
	el:"#app",
	data:{
		msg:"欢迎使用 Vue "
	},
	components:{
		indexV:indexV
	},
	router, // 将路由对象挂载到vue实例上
	
	store
})
